package com.ruoyi.staff.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.staff.mapper.StaffContractMapper;
import com.ruoyi.staff.domain.StaffContract;
import com.ruoyi.staff.service.IStaffContractService;
import com.ruoyi.common.core.text.Convert;

/**
 * 员工合同Service业务层处理
 * 
 * @author bzs
 * @date 2021-06-28
 */
@Service
public class StaffContractServiceImpl implements IStaffContractService 
{
    @Autowired
    private StaffContractMapper staffContractMapper;

    /**
     * 查询员工合同
     * 
     * @param stfId 员工合同ID
     * @return 员工合同
     */
    @Override
    public StaffContract selectStaffContractById(Long stfId)
    {
        return staffContractMapper.selectStaffContractById(stfId);
    }

    /**
     * 查询员工合同列表
     * 
     * @param staffContract 员工合同
     * @return 员工合同
     */
    @Override
    public List<StaffContract> selectStaffContractList(StaffContract staffContract)
    {
        return staffContractMapper.selectStaffContractList(staffContract);
    }

    /**
     * 查询员工合同列表
     * @param stfIds
     * @return
     */
    @Override
    public List<StaffContract> selectStaffContractListByIds(String[] stfIds){
        return staffContractMapper.selectStaffContractListByIds(stfIds);

    }

    /**
     * 新增员工合同
     * 
     * @param staffContract 员工合同
     * @return 结果
     */
    @Override
    public int insertStaffContract(StaffContract staffContract)
    {
        return staffContractMapper.insertStaffContract(staffContract);
    }

    /**
     * 修改员工合同
     * 
     * @param staffContract 员工合同
     * @return 结果
     */
    @Override
    public int updateStaffContract(StaffContract staffContract)
    {
        return staffContractMapper.updateStaffContract(staffContract);
    }

    /**
     * 删除员工合同对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteStaffContractByIds(String[] ids)
    {
        return staffContractMapper.deleteStaffContractByIds(ids);
    }

    /**
     * 删除员工合同信息
     * 
     * @param stfId 员工合同ID
     * @return 结果
     */
    @Override
    public int deleteStaffContractById(Long stfId)
    {
        return staffContractMapper.deleteStaffContractById(stfId);
    }

    /**
     * 导入员工合同关系
     * @param lists
     * @param updateSupport
     * @param sysUser
     * @return
     */
    @Override
    public String importcontractRelations(List<StaffContract> lists, boolean updateSupport, SysUser sysUser) {
        int successNum = 0;
        int failureNum = 0;
        StringBuffer successMsg = new StringBuffer();
        StringBuffer failureMsg = new StringBuffer();
        for (StaffContract sc:lists){
            try {
                //验证是否存在这个员工合同
                StaffContract scontract = staffContractMapper.selectStaffContractByIdCard(sc.getStfIdCard());
                if (StringUtils.isNull(scontract)){
                    staffContractMapper.insertStaffContract(sc);
                    successNum++;
                    successMsg.append("<br/>"+successNum+"、"+sc.getStfIdCard()+"导入成功");
                }else if (updateSupport){
                    staffContractMapper.updateStaffContract(sc);
                    successNum++;
                    successMsg.append("<br/>"+successNum+"、"+sc.getStfIdCard()+"更新成功");
                }else{
                    failureNum++;
                    failureMsg.append("<br/>"+failureNum+"、"+sc.getStfIdCard()+"已存在");
                }
            }catch (Exception e){
                failureNum ++;
                failureMsg.append("<br/>"+failureNum+"、导入失败");
            }
        }
        if(failureNum > 0){
            failureMsg.insert(0, "很抱歉，导入失败！共"+failureNum+"条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }else {
            successMsg.insert(0, "恭喜您数据已全部导入成功！共"+ successNum +"条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     *校验Excel数据是否满足条件
     *
     * @param lists
     * @return
     */
    @Override
    public String validateDatas(List<StaffContract> lists) {
        StringBuilder sb =new StringBuilder();
        if (StringUtils.isEmpty(lists)){
            sb.append("execel中无任何数据，请认证填写！");
        }else {
            for (int i = 0; i <lists.size() ; i++) {
                if (StringUtils.isBlank(lists.get(i).getStfName())){
                    sb.append("第"+(i+1)+"行"+"、姓名不符合标准！<br/>");
                }
                if (StringUtils.isBlank(lists.get(i).getStfDep())){
                    sb.append("第"+(i+1)+"行"+"、部门不符合标准！<br/>");
                }
                if (StringUtils.isBlank(lists.get(i).getStfIdCard().toString())){
                    sb.append("第"+(i+1)+"行"+"、证件号不符合标准！<br/>");
                }
                if (StringUtils.isBlank(lists.get(i).getStfContractId().toString())){
                    sb.append("第"+(i+1)+"行"+"、合同编号不符合标准！<br/>");
                }
            }
        }
        if (sb.length()>0){
            return sb.toString();
        };
        return "";
    }
}
